package com.example.shop_2212

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.mvvm.BaseViewActivity
import com.example.shop_2212.adapter.VpAdapter
import com.example.shop_2212.databinding.ActivityWelBinding
import com.example.shop_2212.welfg.OneFragment
import com.example.shop_2212.welfg.ThreeFragment
import com.example.shop_2212.welfg.TwoFragment
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import java.time.LocalDate

/**
 * 欢迎页，可以是5秒倒计时 ，，有轮播效果的切换
 */
class WelActivity : BaseViewActivity<ActivityWelBinding>() {
    lateinit var vpAdapter: VpAdapter
    val list = mutableListOf<Fragment>()
    override fun initView() {
        //倒计时
      /*  lifecycleScope.launch {
            for (i in 5 downTo 1){
                delay(1000)
                binding.djs.text = "$i 秒"
            }
            ARouter.getInstance().build("/app/MainActivity").navigation()
        }
        binding.djs.setOnClickListener {
            //直接跳过倒计时，进入主页
            finish()
            ARouter.getInstance().build("/app/MainActivity").navigation()
        }*/
        list.add(OneFragment())
        list.add(TwoFragment())
        list.add(ThreeFragment())
        vpAdapter = VpAdapter(this,list)
        binding.vp.adapter = vpAdapter
        //滑动vp,rg 变化
        binding.vp.registerOnPageChangeCallback(object :ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when(position){
                    0->binding.rg.check(R.id.rb1)
                    1->binding.rg.check(R.id.rb2)
                    2->binding.rg.check(R.id.rb3)
                }
            }
        })
        //rg点击，vp 变化
        binding.rg.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId){
                R.id.rb1->binding.vp.currentItem = 0
                R.id.rb2->binding.vp.currentItem = 1
                R.id.rb3->binding.vp.currentItem = 2
            }
        }

        lifecycleScope.launch{
            val flow = flow<Int> {
                emit(1)
            }.collect{
                Log.d("sss", "initView: 接受到的数据是$it")
            }
        }
        val list = arrayListOf<Int>()
        for (i in 1..10){
            list.add(i)
        }
        val intList = list.filter {
            it % 2 == 0
        }
        Log.d("sss", "initView: 偶数为$intList")

        Log.d("sss", "initView:扩展函数 ${"hello".strCount("l")}")
        Log.d("sss", "initView: 内联函数${inlineSum(mutableListOf(1,2,3),0)}")
        val person = Person("zhangsan",18,"ahjfrio")
        updatePersonDetails(person)
        Log.d("sss", "initView:修改后的实体类$person ")
    }

    override fun initData() {

    }

    override fun getLayout(): Int  = R.layout.activity_wel

   fun String.strCount(str:String):Int{
       val toList = this.toCharArray().toList()
       var count = 0
       toList.forEach {
           if(it.toString()==str){
               count++
           }
       }
       return count
   }

    inline fun inlineSum(list: MutableList<Int>,count:Int):Int{
        var count1 = count
        list.forEach {
            count1+=it
        }
        return count1
    }
    fun updatePersonDetails(person: Person){
        with(person){
            name = name.uppercase()
            age += 1
        }
    }

}

data class Person(var name:String,var age:Int,var address:String)